﻿using Md.Api.Entity.ModelDB;
using PetaPoco.NetCore;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 系统岗位仓储
/// </summary>
public class SysPostRep : BaseRep<SysPostModel>
{
    /// <summary>
    /// 通过岗位编号获取系统岗位信息
    /// </summary>
    /// <param name="postId">岗位编号</param>
    /// <returns>SysPostModel</returns>
    public SysPostModel GetSysPostByPostId(long postId)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select `post_id`
                  ,`post_code`
                  ,`post_name`
                  ,`post_sort`
                  ,`status`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_post`
            where 1 = 1 and `post_id` = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<SysPostModel>(sqlStr, postId);
    }
    /// <summary>
    /// 获取系统岗位列表数据
    /// </summary>
    /// <param name="model">筛选条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="beginTime">开始时间</param>
    /// <param name="endTime">结束时间</param>
    /// <returns>IEnumerable<SysPostModel></returns>
    public IEnumerable<SysPostModel> GetSysPostList(SysPostModel model, string? orderBy, bool? isDesc, DateTime? beginTime, DateTime? endTime)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.PostId > 0)
        {
            sqlWhere += " and post_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.PostCode))
        {
            sqlWhere += " and post_code like @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.PostName))
        {
            sqlWhere += " and post_name like  @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @5 ";
        }
        if (beginTime.HasValue)
        {
            sqlWhere += " and create_time >= @6 ";
        }
        if (endTime.HasValue)
        {
            sqlWhere += " and create_time <= @7 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `post_id`
                  ,`post_code`
                  ,`post_name`
                  ,`post_sort`
                  ,`status`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_post`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Query<SysPostModel>(
            sqlStr,
            model.PostId,
            string.Format(@"%{0}%", model.PostCode),
            string.Format(@"%{0}%", model.PostName),
            model.Status,
            model.CreateBy,
            model.UpdateBy,
            string.Format(@"{0} 00:00:00", beginTime),
            string.Format(@"{0} 23:59:59", endTime));
    }
    /// <summary>
    /// 分页获取系统岗位列表数据
    /// </summary>
    /// <param name="model">筛选条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="beginTime">开始时间</param>
    /// <param name="endTime">结束时间</param>
    /// <returns>Page<SysPostModel></returns>
    public Page<SysPostModel> GetSysPostPageList(SysPostModel model, int pageIndex, int pageSize, string? orderBy, bool? isDesc, DateTime? beginTime, DateTime? endTime)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.PostId > 0)
        {
            sqlWhere += " and post_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.PostCode))
        {
            sqlWhere += " and post_code like @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.PostName))
        {
            sqlWhere += " and post_name like  @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @5 ";
        }
        if (beginTime.HasValue)
        {
            sqlWhere += " and create_time >= @6 ";
        }
        if (endTime.HasValue)
        {
            sqlWhere += " and create_time <= @7 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `post_id`
                  ,`post_code`
                  ,`post_name`
                  ,`post_sort`
                  ,`status`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_post`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Page<SysPostModel>(
            pageIndex,
            pageSize,
            sqlStr,
            model.PostId,
            string.Format(@"%{0}%", model.PostCode),
            string.Format(@"%{0}%", model.PostName),
            model.Status,
            model.CreateBy,
            model.UpdateBy,
            string.Format(@"{0} 00:00:00", beginTime),
            string.Format(@"{0} 23:59:59", endTime));
    }
    /// <summary>
    /// 通过岗位编号更新系统岗位
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysPostByRoleId(SysPostModel model)
    {
        var sql = new Sql();

        sql.Append(" update `sys_post` set ");

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.PostCode))
        {
            sql.Append(" post_code = @0, ", model.PostCode);
        }

        if (!string.IsNullOrWhiteSpace(model.PostName))
        {
            sql.Append(" post_name = @0, ", model.PostName);
        }

        if (model.PostSort > -1)
        {
            sql.Append(" post_sort = @0, ", model.PostSort);
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sql.Append(" status = @0, ", model.Status);
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sql.Append(" update_by = @0, ", model.UpdateBy);
        }

        if (!string.IsNullOrWhiteSpace(model.Remark))
        {
            sql.Append(" remark = @0, ", model.Remark);
        }

        sql.Append(" update_time = sysdate() ");
        #endregion

        sql.Append(" where 1 = 1 and post_id = @0 ", model.PostId);

        return MdTrainingEducationDB.GetInstance().Execute(sql);
    }
}

